package com.lwq.controller;

import javax.swing.JFileChooser;
import javax.swing.table.DefaultTableModel;

import com.lwq.model.SuperTableItemModel;
import com.lwq.model.SuperTableModel;
import com.lwq.util.Constance;
import com.lwq.util.GsonUtil;
import com.lwq.view.CreateProDialog;
import com.lwq.view.CreateModelView;
import com.lwq.view.DesignView;

public class ToolsController extends BaseController {
	public static String filePath = null;

	public static void newEditModel() {
		final JFileChooser fc = new JFileChooser();
		fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int returnVal = fc.showOpenDialog(DesignView.getInstance());
		if (fc.getSelectedFile() != null) {
			filePath = fc.getSelectedFile().getPath();
			CreateModelView.getInstance();
		}
	}

	public static void setExistTableModel(DefaultTableModel tableModel,String filePath) {
		if (filePath != null) {
			SuperTableModel stModel = null;
			SuperTableItemModel itemModel = null;
			stModel = (SuperTableModel) GsonUtil.getInstance(GsonUtil.BUILD_TYPE).FileToModel(filePath,
					SuperTableModel.class);
			if (stModel != null) {
				for (int i = 0; i < stModel.itemCount(); i++) {
					itemModel = stModel.getItem(i);
					String[] data = { itemModel.getType(), itemModel.getKey(),
							itemModel.getValue() };
					tableModel.addRow(data);
				}
			}
		}
	}

	public static void creatEditModel(DefaultTableModel model) {
		if (model != null && filePath != null) {
			String str1 = null;
			String str2 = null;
			String str3 = null;
			String[] strs = null;
			SuperTableModel stModel = new SuperTableModel();
			SuperTableItemModel itemModel = null;
			for (int i = 0; i < model.getRowCount(); i++) {
				str1 = (String) model.getValueAt(i, 0);
				str2 = (String) model.getValueAt(i, 1);
				str3 = (String) model.getValueAt(i, 2);
				itemModel = new SuperTableItemModel();
				itemModel.setType(str1);
				itemModel.setKey(str2);
				itemModel.setValue(str3);
				stModel.addItem(itemModel);
			}
			GsonUtil.getInstance(GsonUtil.BUILD_TYPE).modelToFile(stModel, filePath);
		}
	}
}
